/**
 * Created by zxk on 16/3/11.
 */

$(document).ready(function (){
  var lat=getQueryParas(location.search)['lat'];
  var lng=getQueryParas(location.search)['lng'];
  var openId=getQueryParas(location.search)['openId'];
  var address=decodeURI(getQueryParas(location.search)['address']);
      if(lat && lng){
        $("#address").text(address);
            $.ajax({
                type: "POST",
                async: true,
                data: {
                	lat:lat,
                	lng:lng,
                	address:address,
                	openId:openId
                },
                dataType: "json",
                url: "/destination/view_destination",
                success: function(result){
                    if(result.code == 200){
                        initMap(lat, lng);
                        alertSuccess("发送成功",address);
                    }else if(result.code == 401){
                        alertWarn(false, "您可能重新绑定了终端, 请重试!");
                    }else{
                        initMap(lat, lng);
                        alertWarn(false, "发送失败");
                    }
                }
            })
    }else{
        alertWarn("警告", "未找到地址信息");
    }
})
function getQueryParas(url) {
    var queryParas = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.split("?")[1];
        var strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            queryParas[strs[i].split("=")[0]]=strs[i].split("=")[1];
        }
    }
    return queryParas;
}


function initMap(lat, lng){
    var center = new qq.maps.LatLng(lat,lng);
    //显示地图

    $("#map-container").show();
    map = new qq.maps.Map(document.getElementById("map-container"), {
        center: center,
        zoom:12,
    });
    marker = new qq.maps.Marker({
        position: center,
        map: map,
        disableDefaultUI: true
    });
}
function alertSuccess(title, content){
    var jc = $.confirm({
        title: title,
        icon: 'glyphicon glyphicon-ok',
        content: content,
        //confirmButtonClass: "btn-success",
        confirmButton: '确定',
        cancelButton: false
    })
    jc.$content.addClass("text-center");
    jc.$content.css({"color":"#9A3036","font-size":"16px"});
};
function alertWarn(title, content){
    var jc = $.confirm({
        title: title,
        icon: 'glyphicon glyphicon-warning-sign',
        content: content,
        //confirmButtonClass: "btn-success",
        confirmButton: '关闭',
        cancelButton: false
    })
    jc.$content.addClass("text-center");
    jc.$content.css({"color":"#9A3036","font-size":"16px"});
};